home *** CD-ROM | disk | FTP | other *** search
/ United Public Domain Gold 2 / United Public Domain Gold 2.iso / utilities / pu247.dms / pu247.adf / Intuition / Gadgets / Example7.c < prev    next >
C/C++ Source or Header  |  1992-05-01  |  10KB  |  276 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE) V3.0      Amiga C Club (ACC) */
  4. /* -------------------------------      ------------------ */
  5. /*                                                         */
  6. /* Book:    ACM Intuition               Amiga C Club       */
  7. /* Chapter: Gadgets                     Tulevagen 22       */
  8. /* File:    Example7.c                  181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    92-05-01                                       */
  11. /* Version: 1.10                                           */
  12. /*                                                         */
  13. /*   Copyright 1992, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20. /* This program will open a normal window which is connected to the   */
  21. /* Workbench Screen. The window will use all System Gadgets, and will */
  22. /* close first when the user has selected the System gadget Close     */
  23. /* window. Inside the window we have put a String gadget.             */
  24.  
  25.  
  26.  
  27. #include <intuition/intuition.h>
  28.  
  29.  
  30.  
  31. struct IntuitionBase *IntuitionBase;
  32.  
  33.  
  34.  
  35. /* THE STRING GADGET's STRUCTURES: */
  36.  
  37. /* The coordinates for the box: */
  38. SHORT my_points[]=
  39. {
  40.    -7, -4, /* Start at position (-7, -4) */
  41.   200, -4, /* Draw a line to the right to position (200,-4) */
  42.   200, 11, /* Draw a line down to position (200,11) */
  43.    -7, 11, /* Draw a line to the right to position (-7,11) */
  44.    -7, -4  /* Finish of by drawing a line up to position (-7,-4) */ 
  45. };
  46.  
  47. /* The Border structure: */
  48. struct Border my_border=
  49. {
  50.   0, 0,        /* LeftEdge, TopEdge. */
  51.   1,           /* FrontPen, colour register 1. */
  52.   0,           /* BackPen, for the moment unused. */
  53.   JAM1,        /* DrawMode, draw the lines with colour 1. */
  54.   5,           /* Count, 5 pair of coordinates in the array. */
  55.   my_points,   /* XY, pointer to the array with the coordinates. */
  56.   NULL,        /* NextBorder, no other Border structures are connected. */
  57. };
  58.  
  59.  
  60.  
  61. /* The IntuiText structure: */
  62. struct IntuiText my_text=
  63. {
  64.   1,         /* FrontPen, colour register 1. */
  65.   0,         /* BackPen, colour register 0. */
  66.   JAM1,      /* DrawMode, draw the characters with colour 1, do not */
  67.              /* change the background. */ 
  68.   -53, 0,    /* LeftEdge, TopEdge. */
  69.   NULL,      /* ITextFont, use default font. */
  70.   "Text:",   /* IText, the text that will be printed. */
  71.   NULL,      /* NextText, no other IntuiText structures. */
  72. };
  73.  
  74.  
  75.  
  76. UBYTE my_buffer[50]; /* 50 characters including the NULL-sign. */
  77. UBYTE my_undo_buffer[50]; /* Must be at least as big as my_buffer. */
  78.  
  79.  
  80.  
  81. struct StringInfo my_string_info=
  82. {
  83.   my_buffer,       /* Buffer, pointer to a null-terminated string. */
  84.   my_undo_buffer,  /* UndoBuffer, pointer to a null-terminated string. */
  85.                    /* (Remember my_buffer is equal to &my_buffer[0]) */
  86.   0,               /* BufferPos, initial position of the cursor. */
  87.   50,              /* MaxChars, 50 characters + null-sign ('\0'). */
  88.   0,               /* DispPos, first character in the string should be */
  89.                    /* first character in the display. */
  90.  
  91.   /* Intuition initializes and maintaines these variables: */
  92.  
  93.   0,               /* UndoPos */
  94.   0,               /* NumChars */
  95.   0,               /* DispCount */
  96.   0, 0,            /* CLeft, CTop */
  97.   NULL,            /* LayerPtr */
  98.   NULL,            /* LongInt */
  99.   NULL,            /* AltKeyMap */
  100. };
  101.  
  102.  
  103. struct Gadget my_gadget=
  104. {
  105.   NULL,          /* NextGadget, no more gadgets in the list. */
  106.   68,            /* LeftEdge, 68 pixels out. */
  107.   30,            /* TopEdge, 30 lines down. */
  108.   198,           /* Width, 198 pixels wide. */
  109.   8,             /* Height, 8 pixels lines heigh. */
  110.   GADGHCOMP,     /* Flags, draw the select box in the complement */
  111.                  /* colours. Note: it actually only the cursor which */
  112.                  /* will be drawn in the complement colours (yellow). */
  113.                  /* If you set the flag GADGHNONE the cursor will not be */
  114.                  /* highlighted, and the user will therefore not be able */
  115.                  /* to see it. */
  116.   GADGIMMEDIATE| /* Activation, our program will recieve a message when */
  117.   RELVERIFY,     /* the user has selected this gadget, and when the user */
  118.                  /* has released it. */ 
  119.   STRGADGET,     /* GadgetType, a String gadget. */
  120.   (APTR) &my_border, /* GadgetRender, a pointer to our Border structure. */
  121.   NULL,          /* SelectRender, NULL since we do not supply the gadget */
  122.                  /* with an alternative image. */
  123.   &my_text,      /* GadgetText, a pointer to our IntuiText structure. */
  124.   NULL,          /* MutualExclude, no mutual exclude. */
  125.   (APTR) &my_string_info, /* SpecialInfo, a pointer to a StringInfo str. */
  126.   0,             /* GadgetID, no id. */
  127.   NULL           /* UserData, no user data connected to the gadget. */
  128. };
  129.  
  130.  
  131.  
  132. /* Declare a pointer to a Window structure: */ 
  133. struct Window *my_window;
  134.  
  135. /* Declare and initialize your NewWindow structure: */
  136. struct NewWindow my_new_window=
  137. {
  138.   50,            /* LeftEdge    x position of the window. */
  139.   25,            /* TopEdge     y positio of the window. */
  140.   320,           /* Width       320 pixels wide. */
  141.   100,           /* Height      100 lines high. */
  142.   0,             /* DetailPen   Text should be drawn with colour reg. 0 */
  143.   1,             /* BlockPen    Blocks should be drawn with colour reg. 1 */
  144.   CLOSEWINDOW|   /* IDCMPFlags  The window will give us a message if the */
  145.                  /*             user has selected the Close window gad, */
  146.   GADGETDOWN|    /*             or a gadget has been pressed on, or */
  147.   GADGETUP,      /*             a gadge has been released. */
  148.   SMART_REFRESH| /* Flags       Intuition should refresh the window. */
  149.   WINDOWCLOSE|   /*             Close Gadget. */
  150.   WINDOWDRAG|    /*             Drag gadget. */
  151.   WINDOWDEPTH|   /*             Depth arrange Gadgets. */
  152.   WINDOWSIZING|  /*             Sizing Gadget. */
  153.   ACTIVATE,      /*             The window should be Active when opened. */
  154.   &my_gadget,    /* FirstGadget A pointer to the String gadget. */
  155.   NULL,          /* CheckMark   Use Intuition's default CheckMark. */
  156.   "String Window",/* Title       Title of the window. */
  157.   NULL,          /* Screen      Connected to the Workbench Screen. */
  158.   NULL,          /* BitMap      No Custom BitMap. */
  159.   320,           /* MinWidth    We will not allow the window to become */
  160.   50,            /* MinHeight   smaller than 320 x 50, and not bigger */
  161.   640,           /* MaxWidth    than 640 x 200. */
  162.   200,           /* MaxHeight */
  163.   WBENCHSCREEN   /* Type        Connected to the Workbench Screen. */
  164. };
  165.  
  166.  
  167.  
  168. main()
  169. {
  170.   /* Boolean variable used for the while loop: */
  171.   BOOL close_me;
  172.  
  173.   /* Declare a variable in which we will store the IDCMP flag: */
  174.   ULONG class;
  175.  
  176.   /* Declare a pointer to an IntuiMessage structure: */
  177.   struct IntuiMessage *my_message;
  178.  
  179.  
  180.  
  181.   /* Put some text into the my_buffer string: */
  182.   strcpy( my_buffer, "Some text" );
  183.  
  184.  
  185.  
  186.   /* Before we can use Intuition we need to open the Intuition Library: */
  187.   IntuitionBase = (struct IntuitionBase *)
  188.     OpenLibrary( "intuition.library", 0 );
  189.   
  190.   if( IntuitionBase == NULL )
  191.     exit(); /* Could NOT open the Intuition Library! */
  192.  
  193.  
  194.  
  195.   /* We will now try to open the window: */
  196.   my_window = (struct Window *) OpenWindow( &my_new_window );
  197.   
  198.   /* Have we opened the window succesfully? */
  199.   if(my_window == NULL)
  200.   {
  201.     /* Could NOT open the Window! */
  202.     
  203.     /* Close the Intuition Library since we have opened it: */
  204.     CloseLibrary( IntuitionBase );
  205.  
  206.     exit();  
  207.   }
  208.  
  209.  
  210.  
  211.   /* We have opened the window, and everything seems to be OK. */
  212.  
  213.  
  214.  
  215.   close_me = FALSE;
  216.  
  217.   /* Stay in the while loop until the user has selected the Close window */
  218.   /* gadget: */
  219.   while( close_me == FALSE )
  220.   {
  221.     /* Wait until we have recieved a message: */
  222.     Wait( 1 << my_window->UserPort->mp_SigBit );
  223.  
  224.     /* Collect the message: */
  225.     my_message = (struct IntuiMessage *) GetMsg( my_window->UserPort );
  226.  
  227.     /* Have we collected the message sucessfully? */
  228.     if(my_message)
  229.     {
  230.       /* After we have collected the message we can read it, and save any */
  231.       /* important values which we maybe want to check later: */
  232.       class = my_message->Class;      /* Save the IDCMP flag. */
  233.   
  234.       /* After we have read it we reply as fast as possible: */
  235.       /* REMEMBER! Do never try to read a message after you have replied! */
  236.       /* Some other process has maybe changed it. */
  237.       ReplyMsg( my_message );
  238.   
  239.       /* Check which IDCMP flag was sent: */
  240.       switch( class )
  241.       {
  242.         case CLOSEWINDOW:  /* The user selected the Close window gadget! */
  243.                close_me=TRUE;
  244.                break;
  245.                
  246.         case GADGETDOWN:   /* The user has selected the String gadget: */
  247.                            /* (Clicked inside the select box) */
  248.                printf("String gadget selected.\n");
  249.                break;
  250.                
  251.         case GADGETUP:     /* The user has released the String gadget: */
  252.                            /* (Pressed ENTER or RETURN) */
  253.                printf("String gadget released.\n");
  254.                break;
  255.       }
  256.     }
  257.   }
  258.  
  259.  
  260.  
  261.   /* Print out the final string: */
  262.   printf("String: %s\n", my_string_info.Buffer);
  263.  
  264.  
  265.  
  266.   /* We should always close the windows we have opened before we leave: */
  267.   CloseWindow( my_window );
  268.  
  269.  
  270.  
  271.   /* Close the Intuition Library since we have opened it: */
  272.   CloseLibrary( IntuitionBase );
  273.  
  274.   /* THE END */
  275. }
  276.